#!/usr/bin/env bash
set -euo pipefail

##? brew formula

export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
source "${NAVI_HOME}/scripts/install"

gen_formula() {
   version="$(latest_version_released)"
   header "version: ${version}"

   header "sha_for x86_64-unknown-linux-musl..."
   sha_linux="$(sha_for_asset_on_github "$version" "x86_64-unknown-linux-musl")"
   header "$sha_linux"

   header "sha_for x86_64-osx..."
   sha_osx="$(sha_for_asset_on_github "$version" "x86_64-osx")"
   header "$sha_osx"

   header "rb..."
   curl -s https://raw.githubusercontent.com/denisidoro/homebrew-tools/master/navi.rb \
      | sed -E "s/version ['\"].*/version '${version}'/" \
      | awk '!x{x=sub("sha256","sha_osx")}7' \
      | awk '!x{x=sub("sha256","sha_linux")}7' \
      | sed -E "s/sha_osx.*/sha256 \"${sha_osx}\"/" \
      | sed -E "s/sha_linux.*/sha256 \"${sha_linux}\"/"
}

gen_sha() {
   version="${1:-$(latest_version_released)}"
   header "version: ${version}"

   header "sha_for source..."
   sha_for_asset_on_github "$version"
}

cmd="$1"
shift

case "$cmd" in
   "formula") gen_formula "$@" ;;
   "sha") gen_sha "$@" ;;
esac
